package com.hanggold.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * TODO COMMENT.
 *
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2021-11-25 21:58
 * @since 1.0.0
 **/
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
    
    private static final Logger log = LoggerFactory.getLogger(EchoServerHandler.class);
    
    @Override
    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
        log.debug("mgs: {}, type: {}", msg, msg.getClass());
        
        ByteBuf byteBuf = (ByteBuf) msg;
        
        final byte[] array = byteBuf.array();
        
        for (byte b : array) {
            
            log.debug("b: {}" ,b);
        }
        
        ctx.fireChannelRead(msg);
    }
    
    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
        log.info("active");
    }
    
    @Override
    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
        log.error("接受异常： ", cause);
    }
}
